package com.hamizhong.iflowers.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.BeanUtils;

import com.hamizhong.iflowers.db.dao.hibernate.ICityDao;
import com.hamizhong.iflowers.db.dao.hibernate.IProvinceDao;
import com.hamizhong.iflowers.db.entity.City;
import com.hamizhong.iflowers.db.entity.Province;
import com.hamizhong.iflowers.service.ICityService;
import com.hamizhong.iflowers.service.base.MyBaseService;
import com.hamizhong.iflowers.web.dto.IOPResultDto;
import com.hamizhong.iflowers.web.vo.CityVO;
import com.hamizhong.iflowers.web.vo.IObjectVO;
import com.hamizhong.iflowers.web.vo.ProvinceVO;

public class CityServiceImpl extends MyBaseService implements ICityService {
	
	private IProvinceDao iProvinceDao = null;
	private ICityDao iCityDao = null;

	public IProvinceDao getiProvinceDao() {
		return iProvinceDao;
	}
	public void setiProvinceDao(IProvinceDao iProvinceDao) {
		this.iProvinceDao = iProvinceDao;
	}
	public ICityDao getiCityDao() {
		return iCityDao;
	}
	public void setiCityDao(ICityDao iCityDao) {
		this.iCityDao = iCityDao;
	}
	
	public IOPResultDto<IObjectVO> findByProvinceId(String provinceId) {
		IOPResultDto<IObjectVO> dto = new IOPResultDto<IObjectVO>();
		Province province = iProvinceDao.findById(Integer.valueOf(provinceId));
		HashMap<String, Object> param = new HashMap<String, Object>();
		param.put("province", province);
		List<City> cityList = iCityDao.findByExample(param);
		List<CityVO> cityVOList = new ArrayList<CityVO>();
		CityVO cityVO = null;
		for (City city : cityList) {
			cityVO = new CityVO();
			BeanUtils.copyProperties(city, cityVO);
			cityVOList.add(cityVO);
		}
		IObjectVO iObjectVO = new IObjectVO();
		iObjectVO.setCityVOList(cityVOList);
		dto.addToResultList(iObjectVO);
		return dto;
	}
	
	public IOPResultDto<IObjectVO> findById(Integer cityId) {
		IOPResultDto<IObjectVO> dto = new IOPResultDto<IObjectVO>();
		List<CityVO> cityVOList = new ArrayList<CityVO>();
		CityVO cityVO = new CityVO();
		ProvinceVO provinceVO = new ProvinceVO();
		
		City city = iCityDao.findById(cityId);
		BeanUtils.copyProperties(city, cityVO);
		Province province = iProvinceDao.findById(city.getProvince().getProvinceId());
		BeanUtils.copyProperties(province, provinceVO);
		cityVO.setProvinceVO(provinceVO);
		cityVOList.add(cityVO);
		
		IObjectVO iObjectVO = new IObjectVO();
		iObjectVO.setCityVOList(cityVOList);
		dto.addToResultList(iObjectVO);
		return dto;
	}
	
}
